package com.info33.platform.system.controller;

import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dto.SysConfigDTO;
import com.info33.platform.system.dto.SysUserDTO;
import com.info33.platform.system.entity.SysConfig;
import com.info33.platform.system.service.SysConfigService;
import com.info33.platform.system.service.SysLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/SysLogin")
@Api(tags = "系统 - 登录")
public class SysLoginController {

    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SysConfigService sysConfigService;

    @PostMapping(value = "/pc/config")
    @ApiOperation(value = "pc - 获取配置")
    public Result<SysConfig> pcConfig(
            @Validated(value = SysConfigDTO.PcConfig.class) @RequestBody SysConfigDTO dto) {
        return sysConfigService.pcConfig(dto);
    }

    @PostMapping("/password/login")
    @ApiOperation(value = "pc - 账号密码登录")
    public Result<Object> passwordLogin(@Validated(value = SysUserDTO.PasswordLogin.class) @RequestBody SysUserDTO dto) {
        return sysLoginService.passwordLogin(dto);
    }

    @PostMapping("/message/login")
    @ApiOperation(value = "pc - 手机号验证码登录")
    public Result<Object> messageLogin(@Validated(value = SysUserDTO.MessageLogin.class) @RequestBody SysUserDTO dto) {
        return sysLoginService.messageLogin(dto);
    }

    @GetMapping(value = "/qr/generate")
    @ApiOperation(value = "二维码登录", notes = "生成登录二维码")
    public Result<Object> generateQr() {
        return sysLoginService.generateQr();
    }

    @GetMapping(value = "/qr/scan")
    @ApiOperation(value = "二维码登录", notes = "手机扫码")
    public Result<Object> qrScan(
            @RequestParam String uuid) {
        return sysLoginService.qrScan(uuid);
    }

    @GetMapping(value = "/qr/login")
    @ApiOperation(value = "二维码登录", notes = "确认登录")
    public Result<Object> qrLogin(
            @RequestParam String uuid) {
        return sysLoginService.qrLogin(uuid);
    }

    @GetMapping(value = "/qr/polling")
    @ApiOperation(value = "二维码登录", notes = "轮询校验")
    public Result<Object> qrPolling(
            @RequestParam String uuid) {
        return sysLoginService.qrPolling(uuid);
    }

    @PostMapping(value = "/message/push")
    @ApiOperation(value = "pc - 发送验证码")
    public Result<Object> messagePush(
            @Validated(value = SysUserDTO.MessagePush.class) @RequestBody SysUserDTO dto) {
        return sysLoginService.messagePush(dto);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "登出")
    public Result<Object> logout() {
        SecurityUtils.getSubject().logout();
        return Result.ok();
    }

    @PostMapping("/forget/password")
    @ApiOperation(value = "pc - 忘记密码")
    public Result<Object> forgetPassword(
            @Validated(value = SysUserDTO.ForgetPassword.class) @RequestBody SysUserDTO dto) {
        return sysLoginService.forgetPassword(dto);
    }


}